const app = getApp()
import { toast } from '../../../../utils/Tools'
import { _getPosition,handletabChange } from '../../../utils/Tools'
import { getOrderHall,getOrderReceipt } from '../../../network/order.js'
let timer;
let valTimer;
Page({
  data: {
    position: '广州市',
    isModal: false,
    time: null,
    orderHall:[],
    orderPush:{},
    list: [],
    isIPhone: false
  },
  onLoad(){
    this.setData({list:app.globalData.list,isIPhone: app.globalData.isIPhone})
    wx.hideHomeButton()
  },
  onShow() {
    if(!app.globalData.selectCity){  // 判断全局变量有没有selectCity数据
      _getPosition().then(res=>{ // 返回定位城市
        this.setData({
          position: res.result.address_component.city
        })
        this._getOrderHall(this.data.position)
        this.getOrderPush()
        app.globalData.selectCity = res.result.address_component.city
      })
    }else{  //  有selectCity数据
      this.setData({position: app.globalData.selectCity})
      this._getOrderHall(this.data.position)
      this.getOrderPush()
    }
    // this.getOrderPush()
  },
  onHide(){
    clearTimeout(timer);
    clearInterval(valTimer);
  },
  onUnload(){
    clearTimeout(timer);
    clearInterval(valTimer);
    // this.getOrderPush()
  },
  //            事件处理函数            //
  tabChange(e) {
    const {index} = e.detail
    handletabChange(index)
  },
  goTo(){
    wx.navigateTo({
      url:'/driver_side/pages/order-hall/city/city'
    })
  },
  handleGps(e){ //   处理点击导航事件
    wx.navigateTo({
      url:'/pages/other/map/map?address=' + e.currentTarget.dataset.address
    })
  },
  handleCheckDetail(e){  //   处理点击查看详情事件
    wx.navigateTo({
      url: "/driver_side/pages/order-hall/orderDetail/orderDetail?id=" + e.currentTarget.dataset.id 
    })
  },
  handleOrder(e){ //   获取立即接单事件
    clearInterval(valTimer);
    const {id} = e.currentTarget.dataset
    wx.showModal({
      title: '确认提示',
      content: '您是否确认接单',
      success: (result) => {
        if (result.confirm) {
          getOrderReceipt(id).then(res=>{
            if(res.code===200){
              toast('接单成功','success',1500)
              setTimeout(()=>{
                wx.reLaunch({
                  url: '/driver_side/pages/process/index/index'
                });
              },1500)
            }else{
              toast('接单失败','loading')          
            }
          })
        }else{
          toast('您取消了接单')
          this.setData({
            time: null,
            orderPush:[]
          })
          setTimeout(this.getOrderPush,5000)
        }
      }
    });
    wx.requestSubscribeMessage({
      tmplIds: ['R0zgiGQObzSjDFxH3KQjp_wRs1EkFI-l7iegE2Ru678'],
      success:  res=>{
        console.log(res)
      }
    })
  },
  handleModalClose(){   // 点击关闭订单推送
    console.log('关闭订单推送吗，嘻嘻哈哈')
    clearInterval(valTimer)
    this.setData({
      orderPush:[],
      time:null
    })
    setTimeout(this.getOrderPush,5000)
  },

  //             网络请求              //
  _getOrderHall(data){   // 获取订单大厅数据
    getOrderHall(data).then(res=>{
      this.setData({orderHall:res.data})
    })
  },
  getOrderPush(){
    wx.request({
      url:  app.globalData.BASE_URL+'api/driver/order/company_send',
        data: {
          position:this.data.position,
          token: wx.getStorageSync('token')
        },
        timeout:80000,
        success: res => {
          if(res.data.code === 200 && res.data.data){
            this.setData({
              orderPush:res.data.data,
            })
          }
        }
    })
  },
  // getOrderPush(){   // 获取订单推送数据
  //   console.log('真的是在这里吗，傻逼啊啊啊啊啊啊啊 ')
  //   if(!this.data.time){
  //     wx.request({
  //       url:  app.globalData.BASE_URL+'api/driver/order/company_send',
  //       data: {
  //         position:this.data.position,
  //         token: wx.getStorageSync('token')
  //       },
  //       timeout:80000,
  //       success: res => {
  //         console.log(res)
  //         if(res.data.code === 200 && res.data.data){
  //           this.setData({
  //             orderPush:res.data.data,
  //             time: 6
  //           })
  //           console.log(res.data.data,this.data.time)
  //           let number = this.data.time
  //           valTimer = setInterval(() => {
  //             this.setData({
  //               time: --number
  //             })
  //             console.log(this.data.time)
  //             if(this.data.time <= 0){
  //               clearInterval(valTimer)
  //               this.setData({
  //                 orderPush:[]
  //               })
  //               timer = setTimeout(this.getOrderPush,5000)
  //             }
  //           }, 1000);
  //         }else{
  //           console.log('有循环在这里吗，我真他吗的烦死了')
  //           timer = setTimeout(this.getOrderPush,5000)
  //         }
  //       }
  //     });
  //   }
  // }
})
